package com.fromfuture.xuhuidoc.android.base;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.webkit.ValueCallback;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.fromfuture.xuhuidoc.R;
import com.fromfuture.xuhuidoc.android.commonresouce.MyHtml;

import java.util.Map;

import app.auto.runner.ActivityBaseCompact;
import app.auto.runner.base.intf.ListBuilder;
import app.auto.runner.base.intf.MapBuilder;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class BaseWebActivity extends ActivityBaseCompact {
    @BindView(R.id.BaseWebview_toolbar)
    RelativeLayout BaseWebview_toolbar;
    @BindView(R.id.BaseWebActivity_back)
    ImageView BaseWebActivity_back;
    @BindView(R.id.BaseWebActivity_title)
    TextView BaseWebActivity_title;
    @BindView(R.id.BaseWebActivity_save)
    TextView BaseWebActivity_save;
    @BindView(R.id.BaseWebActivity_webView)
    MyWebview BaseWebActivity_webView;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_base_web);
        ButterKnife.bind(this);
        init();
    }

    @Override
    public void onBackPressed() {
        if (BaseWebActivity_webView.canGoBack()) {
            BaseWebActivity_webView.goBack();
        } else {
            super.onBackPressed();
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        switch (requestCode) {
            case AppManager.type_creathealthProgram:
                if (resultCode == Activity.RESULT_OK) {
                    BaseWebActivity_webView.reload();
                }
                break;
            case AppManager.type_creatattentionCase:
                if (resultCode == RESULT_OK) {
                    BaseWebActivity_webView.reload();
                }
                break;
        }
        String page = null;
        if (data != null && (page = data.getStringExtra("page")) == null) {
            if (page.equals("")) {

            }
        }
        if (BaseWebActivity_webView.getUrl().toLowerCase().contains("newrecord")) {
            BaseWebActivity_webView.loadUrl("javascript:load('true');");
        }
    }


    @OnClick(R.id.BaseWebActivity_back)
    public void BaseWebActivity_back() {
        if (BaseWebActivity_webView.canGoBack()) {
            BaseWebActivity_webView.goBack();
        } else {
            super.onBackPressed();
        }
    }

    @OnClick(R.id.BaseWebActivity_save)
    public void BaseWebActivity_save() {
        if (BaseWebActivity_title.getText().equals("创建健康方案模板")) {
            BaseWebActivity_webView.evaluateJavascript("", new ValueCallback<String>() {
                @Override
                public void onReceiveValue(String value) {

                }
            });
        } else if (BaseWebActivity_title.getText().equals("创建注意事项模板")) {
            BaseWebActivity_webView.evaluateJavascript("", new ValueCallback<String>() {
                @Override
                public void onReceiveValue(String value) {

                }
            });
        } else if (BaseWebActivity_title.getText().equals("编辑健康方案模板")) {
            BaseWebActivity_webView.evaluateJavascript("", new ValueCallback<String>() {
                @Override
                public void onReceiveValue(String value) {

                }
            });
        } else if (BaseWebActivity_title.getText().equals("编辑注意事项模板")) {
            BaseWebActivity_webView.evaluateJavascript("", new ValueCallback<String>() {
                @Override
                public void onReceiveValue(String value) {

                }
            });
        }
    }


    public void injectTitle(final String title) {
        runOnUiThread(new Runnable() {
            @SuppressLint("WrongConstant")
            @Override
            public void run() {
                Map m = MapBuilder.build().add("会诊说明", "submit").add("创建多人会诊", "submit").get();
                if (ListBuilder.build().add("多人会诊").get().contains(BaseWebActivity_title.getText().toString())) {
                    BaseWebActivity_save.setVisibility(View.INVISIBLE);
                }
                BaseWebActivity_title.setText(title);
                final String command = (String) m.get(BaseWebActivity_title.getText().toString());
                if (!TextUtils.isEmpty(command)) {

                    BaseWebActivity_save.setVisibility(View.VISIBLE);
                    if (command.contains("local:")) {

                    } else {
                        BaseWebActivity_save.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                BaseWebActivity_webView.loadUrl("javascript:" + command + "()");

                            }
                        });
                    }
                }


                //back
                if (!TextUtils.isEmpty(BaseWebActivity_title.getText().toString())) {

                    if (BaseWebActivity_title.getText().toString().contains("多人会诊")) {

                        BaseWebActivity_back.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                ((Activity) v.getContext()).finish();
                            }
                        });
                    }
                } else {
                    BaseWebActivity_back.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            onBackPressed();
                        }
                    });
                }
            }
        });

    }

    private void init() {
        Intent intent = getIntent();
        String name = intent.getStringExtra("name");
        final String couldsave = intent.getStringExtra("save_js");
        if (name != null && !name.toLowerCase().equals("")) {
            BaseWebActivity_webView.loadUrl(MyHtml.COMMONURL + name);
            BaseWebview_toolbar.setVisibility(View.VISIBLE);
            if (couldsave != null) {
                BaseWebActivity_save.setVisibility(View.VISIBLE);
                BaseWebActivity_save.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        BaseWebActivity_webView.loadUrl("javascript:" + couldsave + "()");
                    }
                });
            } else {
                BaseWebActivity_save.setVisibility(View.INVISIBLE);
            }
            return;
        }
        switch (intent.getIntExtra("type", -1)) {
            case AppManager.type_attentionCase:

                break;
            case AppManager.type_change_pwd:
                BaseWebActivity_webView.loadUrl(MyHtml.COMMONURL + "change_pwd.html");
                BaseWebview_toolbar.setVisibility(View.VISIBLE);
                BaseWebActivity_save.setVisibility(View.GONE);
                break;
            case AppManager.type_choiceDoc:

                break;
            case AppManager.type_choiceMechanism:

                break;
            case AppManager.type_choicePatient://5

                break;
            case AppManager.type_closeConsultation:
                break;
            case AppManager.type_consultationexplain:

                break;
            case AppManager.type_multiple_inquire:

                BaseWebActivity_webView.loadUrl(MyHtml.COMMONURL + "waitConsultation.html");

                BaseWebActivity_save.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        BaseWebActivity_webView.loadUrl("javascript:" + "submit" + "()");
                    }
                });
                BaseWebActivity_title.setText("多人会诊");
                break;
            case AppManager.type_creatattentionCase:
                BaseWebActivity_webView.loadUrl(MyHtml.CREATE_TENTION_CASE);
                BaseWebActivity_save.setVisibility(View.VISIBLE);
                break;
            case AppManager.type_createPersonConsul:

                break;
            case AppManager.type_creathealthProgram://10
                BaseWebActivity_webView.loadUrl(MyHtml.CREATE_HEALTH_PROGRAM);
                BaseWebActivity_save.setVisibility(View.VISIBLE);
                break;
            case AppManager.type_drugName:

                break;
            case AppManager.type_editattentionCase:
                BaseWebActivity_webView.loadUrl(MyHtml.EDITE_TENTION_CASE);
                BaseWebActivity_save.setVisibility(View.VISIBLE);
                break;
            case AppManager.type_edithealthProgram:
                BaseWebActivity_webView.loadUrl(MyHtml.EDITE_HEALTH_PROGRAM);
                BaseWebActivity_save.setVisibility(View.VISIBLE);
                break;
            case AppManager.type_forgetPwd:

                break;
            case AppManager.type_forgetPwdPC://15

                break;
            case AppManager.type_healthPlan:

                break;
            case AppManager.type_healthProgram:

                break;
            case AppManager.type_healthProgramMain:

                break;
            case AppManager.type_healthProgramN:

                break;
            case AppManager.type_huizhenDetail://20

                break;
            case AppManager.type_huizhenExplain:

                break;
            case AppManager.type_index:

                break;
            case AppManager.type_inquiry_details:
                BaseWebActivity_webView.loadUrl(MyHtml.INQUIRY_DETAILS);
                BaseWebActivity_save.setVisibility(View.GONE);
                break;
            case AppManager.type_inquiry_record:
                BaseWebActivity_webView.loadUrl(MyHtml.INQUIRY_RECORD);
                BaseWebActivity_save.setVisibility(View.GONE);
                break;
            case AppManager.type_login://25

                break;
            case AppManager.type_medicationPlan:

                break;
            case AppManager.type_my_home:

                break;
            case AppManager.type_new_file:

                break;
            case AppManager.type_newRecord:

                break;
            case AppManager.type_offline_yuyue://30

                break;
            case AppManager.type_tab_webview_subpage_about:

                break;
            case AppManager.type_tab_webview_subpage_chat:

                break;
            case AppManager.type_tab_webview_subpage_setting:

                break;
            case AppManager.type_tigeCheck:

                break;
            case AppManager.type_view_medical_record://35
                BaseWebActivity_webView.loadUrl(MyHtml.VIEW_MEDICAL_RECORD);
                BaseWebActivity_save.setVisibility(View.GONE);
                break;
            case AppManager.type_viewContent:

                break;
            case AppManager.type_waitConsultation:

                break;
            case AppManager.type_yeji_assessment:
                BaseWebActivity_webView.loadUrl(MyHtml.YEJI_ASSESSMENT);
                BaseWebActivity_save.setVisibility(View.GONE);
                break;
            case AppManager.type_zhenshiDetail:

                break;
        }
        BaseWebActivity_webView.setWebViewClient(new WebViewClient() {

            @Override
            public void onPageFinished(WebView view, String url) {
                super.onPageFinished(view, url);
                if (url.contains("change_pwd.html")) {
                    BaseWebActivity_title.setText("修改密码");
                } else if (url.contains("waitConsultation.html")) {
                    BaseWebActivity_title.setText("多人会诊");
                } else if (url.contains("inquiry_record.html")) {
                    BaseWebActivity_title.setText("咨询记录");
                } else if (url.contains("yeji_assessment.html")) {
                    BaseWebActivity_title.setText("业绩考核");
                } else if (url.contains("creathealthProgram.html")) {
                    BaseWebActivity_title.setText("创建健康方案模板");
                } else if (url.contains("creatattentionCase.html")) {
                    BaseWebActivity_title.setText("创建注意事项模板");
                } else if (url.contains("edithealthProgram.html")) {
                    BaseWebActivity_title.setText("编辑健康方案模板");
                } else if (url.contains("editattentionCase.html")) {
                    BaseWebActivity_title.setText("编辑注意事项模板");
                } else if (url.contains("inquiry_details.html")) {
                    BaseWebActivity_title.setText("问诊详情");
                } else if (url.contains("view_medical_record.html")) {
                    BaseWebActivity_title.setText("咨询建议详情");
                }
            }
        });
    }

}
